package jp.co.libgate.controller;

import jp.co.libgate.common.exception.AlreadyInException;
import jp.co.libgate.common.exception.AlreadyOutException;
import jp.co.libgate.model.Attend;
import jp.co.libgate.service.AttendService;

import org.slim3.controller.Navigation;
import org.slim3.util.RequestMap;

import java.util.HashMap;
import java.util.Map;
import java.util.logging.Logger;

public class AttendController extends JsonController {
    private static final Logger log = Logger.getLogger(AttendController.class.getName());

    private AttendService service = new AttendService();

    @Override
    public Navigation run() throws Exception {
        //出退勤 id,時間、場所、フラグ（1・・出社、９・・退社）
        try{
            Attend attend=null;
            attend = service.regist(new RequestMap(request));
            return redirect("/attendInfo?id="+attend.getId());
        }catch(AlreadyInException e){
            Map<String,String> msg = new HashMap<String,String>();
            msg.put("result","false");
            msg.put("msg","出勤済みです");
            json(msg);
            return null;
        }catch(AlreadyOutException e){
            Map<String,String> msg = new HashMap<String,String>();
            msg.put("result","false");
            msg.put("msg","出勤していません");
            json(msg);
            return null;

        }
        //エラーも返すよ
        //１なのに最新レコードが退社入っている場合
        //９なのに最新レコードに出社入っている場合

    }
}
